package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 * @Author: zyx
 * @CreateTime: 2024-10-10
 */
@RestController
@Slf4j
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {
//            先获取文件的名
            String originalFilename = file.getOriginalFilename();
//            获取文件的扩展名
           String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//           拼接
            String ObjectName = UUID.randomUUID() + extension ;
//            获得文件路径
            String filePath = aliOssUtil.upload(file.getBytes(),ObjectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("上传文件失败");
        }
        return null;
    }
}
